﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Xml;
using System.Windows.Forms;

using TLL.UTL;
using TLL.Debug;
using TLL.Repository;
using TLL.Finance.Currencys;


namespace TLL.Finance.BankOrg
{
    public class OrganizationCurrencysRepository : XMLRepository<Organization>
    {
        public OrganizationCurrencysRepository(NotifyIcon notify) : base("http://resources.finance.ua/ru/public/currency-cash.xml", "Debug\\Log.txt", true, true, XMLDirectionType.Web, notify) { }

        public override List<Organization> Get(int id = 0)
        {
            try
            {
                var res = new List<Organization>();
                var cities = GetCitys();
                var regions = GetRegions();
                foreach (XmlNode organization in this.FindNode(this.Document.ChildNodes[1], "organizations").ChildNodes)
                {
                    var currencys = new List<Currency>();
                    foreach (XmlNode currency in this.FindNode(organization, "currencies").ChildNodes)
                        switch (currency.Attributes["id"].Value)
                        {
                            case "EUR": currencys.Add(new Currency(
                                CurrencyType.EUR,
                                double.Parse(currency.Attributes["br"].Value.Replace('.', ',')),
                                double.Parse(currency.Attributes["ar"].Value.Replace('.', ','))));
                                break;
                            case "RUB": currencys.Add(new Currency(
                                CurrencyType.RUB,
                                double.Parse(currency.Attributes["br"].Value.Replace('.', ',')),
                                double.Parse(currency.Attributes["ar"].Value.Replace('.', ','))));
                                break;
                            case "USD": currencys.Add(new Currency(
                                CurrencyType.USD,
                                double.Parse(currency.Attributes["br"].Value.Replace('.', ',')),
                                double.Parse(currency.Attributes["ar"].Value.Replace('.', ','))));
                                break;
                        }

                    res.Add(new Organization(
                        this.FindNode(organization, "title").Attributes["value"] == null ? "None" : this.FindNode(organization, "title").Attributes["value"].Value,
                        this.FindNode(organization, "phone").Attributes["value"] == null ? "None" : this.FindNode(organization, "phone").Attributes["value"].Value,
                        this.FindNode(organization, "address").Attributes["value"] == null ? "None" : this.FindNode(organization, "address").Attributes["value"].Value,
                        cities.FirstOrDefault(x => x.ID == this.FindNode(organization, "city").Attributes["id"].Value),
                        regions.FirstOrDefault(x => x.ID == this.FindNode(organization, "region").Attributes["id"].Value),
                        currencys
                    ));
                }
                return res;
            }
            catch
            {
                this.Error = new Error(ErrorType.Get);
                this.Warning = new Warning(
                    WarningType.Internet,
                    string.Format("Error getting currency organizations"),
                    this.Notify, 
                    delegate(object sender, EventArgs e) {
                        this.Error.Show(DisplayDebugType.MessageBox);
                    });
                return null;
            }
        }
        public List<Region> GetRegions()
        {
            var res = new List<Region>();
            foreach (XmlNode region in this.FindNode(this.Document.ChildNodes[1], "regions").ChildNodes)
                res.Add(new Region(
                    region.Attributes["id"].Value,
                    region.Attributes["title"].Value
                ));
            return res;
        }
        public List<City> GetCitys()
        {
            var res = new List<City>();
            foreach (XmlNode region in this.FindNode(this.Document.ChildNodes[1], "cities").ChildNodes)
                res.Add(new City(
                    region.Attributes["id"].Value,
                    region.Attributes["title"].Value
                ));
            return res;
        }
    }
}
